const ws = require('windows-shortcuts')
const os = require('os')
const path = require('path')
const OS = require('../index')
const cp = require('child_process')
const util = require('util')

class Win32OS extends OS {
    DIRECTORY_KEY = 'HKEY_CURRENT_USER\\Software\\Classes\\Directory\\shell'
    DIRECTORY_BACKGROUND_KEY = 'HKEY_CURRENT_USER\\Software\\Classes\\Directory\\Background\\shell'
    FILE_KEY = 'HKEY_CURRENT_USER\\Software\\Classes\\*\\shell'

    async createShortcut(shortcutFile, target, options) {
        return await new Promise((resolve, reject) => {
            const opts = {
                target: target,
                args: '',
                runStyle: ws.NORMAL,
                desc: ''
            }
            Object.assign(opts, options)
            ws.create(shortcutFile, opts, error => {
                if (error) {
                    return reject(error)
                }
                resolve(shortcutFile)
            })
        })
    }
    async createShortcutToDesktop(shortcutName, target, options) {
        const home = os.homedir()
        const desktopPath = path.join(home, 'Desktop')
        const shortcut = path.join(desktopPath, shortcutName + '.lnk')
        return await this.createShortcut(shortcut, target, options)
    }
    async existsKeyPath(keyPath) {
        const comm = util.format('REG QUERY "%s"', keyPath)
        try {
            await this.exec(comm)
            return true
        } catch (error) {
            return false
        }
    }
    async deleteKeyPath(keyPath) {
        const command = util.format('REG DELETE "%s" /f', keyPath)
        await this.exec(command)
    }
    async createKeyPathIcon(keyPath, icon) {
        const iconCommand = util.format(
            'REG ADD "%s" /v "icon" /t REG_EXPAND_SZ /d "%s" /f',
            keyPath,
            icon
        )
        await this.exec(iconCommand)
    }
    async createKeyPath(keyPath, name, command, icon) {
        if (await this.existsKeyPath(keyPath)) {
            await this.deleteKeyPath(keyPath)
        }
        const nameCommand = util.format('REG ADD "%s" /t REG_EXPAND_SZ /d "%s" /f', keyPath, name)
        await this.exec(nameCommand)
        if (command) {
            await this.createKeyPathCommand(keyPath, command)
        }
        if (icon) {
            await this.createKeyPathIcon(keyPath, icon)
        }
    }
    async createKeyPathCommand(keyPath, command) {
        const comm = util.format(
            'REG ADD "%s\\command" /t REG_EXPAND_SZ /d "%s %V" /f',
            keyPath,
            command
        )
        await this.exec(comm)
    }
    //#region File
    async hasFileMenu(id) {
        const keyPath = util.format('%s\\%s', this.FILE_KEY, id)
        return this.existsKeyPath(keyPath)
    }
    async createFileMenu(id, label, target, icon) {
        const keyPath = util.format('%s\\%s', this.FILE_KEY, id)
        return this.createKeyPath(keyPath, label, target, icon)
    }
    async deleteFileMenu(id) {
        const keyPath = util.format('%s\\%s', this.FILE_KEY, id)
        return await this.deleteKeyPath(keyPath)
    }
    async createFileMenuCommand(id, command) {
        const keyPath = util.format('%s\\%s', this.FILE_KEY, id)
        return await this.createKeyPathCommand(keyPath, command)
    }
    //#endregion

    //#region Path
    async hasPathMenu(id) {
        const keyPath = util.format('%s\\%s', this.DIRECTORY_KEY, id)
        return this.existsKeyPath(keyPath)
    }
    async createPathMenu(id, label, target, icon) {
        const keyPath = util.format('%s\\%s', this.DIRECTORY_KEY, id)
        return this.createKeyPath(keyPath, label, target, icon)
    }
    async deletePathMenu(id) {
        const keyPath = util.format('%s\\%s', this.DIRECTORY_KEY, id)
        return await this.deleteKeyPath(keyPath)
    }
    async createPathMenuCommand(id, command) {
        const keyPath = util.format('%s\\%s', this.DIRECTORY_KEY, id)
        return await this.createKeyPathCommand(keyPath, command)
    }
    //#endregion

    //#region Path-Background
    async hasBackgroundPathMenu(id) {
        const keyPath = util.format('%s\\%s', this.DIRECTORY_BACKGROUND_KEY, id)
        return this.existsKeyPath(keyPath)
    }
    async createBackgroundPathMenu(id, label, command, icon) {
        const keyPath = util.format('%s\\%s', this.DIRECTORY_BACKGROUND_KEY, id)
        return this.createKeyPath(keyPath, label, command, icon)
    }
    async deleteBackgroundPathMenu(id) {
        const keyPath = util.format('%s\\%s', this.DIRECTORY_BACKGROUND_KEY, id)
        return await this.deleteKeyPath(keyPath)
    }
    async createBackgroundPathMenuCommand(id, command) {
        const keyPath = util.format('%s\\%s', this.DIRECTORY_BACKGROUND_KEY, id)
        return await this.createKeyPathCommand(keyPath, command)
    }
    //#endregion
}

module.exports = Win32OS
